﻿using System;
using System.Web;
using Bintje;

public partial class UI_edit_Profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (AppUser.currUser() > 0)
            {
                Bintje.bintje.imageoSession session = (Bintje.bintje.imageoSession)Session["Session"]; 
                Bintje.User userDetail = new Bintje.User(session);
                if (userDetail != null)
                {
                    userDetail.LoadDetails();
                    txtFirstName.Text = userDetail.NickName;
                    txtLastname.Text = userDetail.Login;
                    txtProfileDescription.Text = string.Empty;
                    txtEmail.Text = userDetail.Login;
                    if (!string.IsNullOrEmpty(userDetail.NickName))
                    {
                        lbluser.Text = userDetail.NickName + "'s profile";
                    }

                    imgProfileImage.ImageUrl = userDetail.ImageUrl;
                    if (string.IsNullOrEmpty(imgProfileImage.ImageUrl))
                    {
                        imgProfileImage.ImageUrl = string.Format("~/sitefiles/Get-Thumbnail.aspx?f={0}&w=85&h=85", "~/images/users/anon.JPG");
                    }

                }
            }
            else
            {
                Response.Redirect("../login.aspx?Retuenurl=" + Request.Url.ToString());
            }
        }
    }

    protected void btnSave_click(object sender, EventArgs e)
    {
        if (AppUser.currUser() > 0)
        {
            Bintje.User userDetail = new Bintje.User((Bintje.bintje.imageoSession)Session["Session"]);
            if (userDetail != null)
            {
                userDetail.LoadDetails();
                userDetail.Login = txtLastname.Text.Trim();
                userDetail.NickName = txtFirstName.Text.Trim();

                HttpPostedFile pf = fuprofilePhoto.PostedFile;
                if (pf.ContentType.Equals("image/gif") ||
                    pf.ContentType.Equals("image/jpg") ||
                    pf.ContentType.Equals("image/jpeg") ||
                    pf.ContentType.Equals("image/pjpeg") ||
                    pf.ContentType.Equals("image/png") ||
                    pf.ContentType.Equals("image/bmp"))
                {
                    if (pf.ContentLength > 0)
                    {
                        if (null != userDetail.UserImage)
                        {
                            userDetail.UserImage.ImageData = fuprofilePhoto.FileBytes;
                            userDetail.UserImage.FileName = fuprofilePhoto.FileName;
                        }
                        else
                        {
                            Bintje.Image img = new Bintje.Image();
                            img.FileName = fuprofilePhoto.FileName;
                            img.ImageData = fuprofilePhoto.FileBytes;
                            userDetail.UserImage = img;
                        }
                    }
                }
                userDetail.Update();
            }
            Response.Redirect("~/UI/userDetail.aspx?comm=1");
            lblMsg.Text = "Profile has edited successfully";
        }
    }
}
